package com.blog.wu.myblog.shiro;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.pam.ModularRealmAuthenticator;
import org.apache.shiro.realm.Realm;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Collection;

/**
 * @author wuyanshen
 * @date 2019-01-11 5:03 PM
 * @discription 描述
 */
public class MyModularRealmAuthenticator extends ModularRealmAuthenticator {

    private static final Logger logger = LoggerFactory.getLogger(MyModularRealmAuthenticator.class);

    @Override
    protected AuthenticationInfo doAuthenticate(AuthenticationToken authenticationToken)
            throws AuthenticationException {
        logger.info("MyModularRealmAuthenticator:方法 doAuthenticate() 执行 ");
        // 判断getRealms()是否返回为空
        assertRealmsConfigured();
        // 强制转换回自定义的CustomizedToken
//        UserToken userToken = (UserToken) authenticationToken;
        // 登录类型
//        String loginType = userToken.getLoginType();
        // 所有Realm
        Collection<Realm> realms = getRealms();
        // 登录类型对应的所有Realm

        // 判断是单Realm还是多Realm
        if (realms.size() == 1){
            logger.info("单realm doSingleRealmAuthentication() 执行 ");
            return doSingleRealmAuthentication((Realm)realms.iterator().next(), authenticationToken);
        }else{
            logger.info("多realm doMultiRealmAuthentication() 执行 ");
            return doMultiRealmAuthentication(realms, authenticationToken);
        }

    }
}
